package com.dingding.order.core.history.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.base.daox.ISQLBaseDao;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.core.history.dao.IHistoryOrderDao;
import com.dingding.order.core.history.dto.TakeMileageDTO;
import com.dingding.order.core.history.sql.HistoryOrderSQL;
import com.dingding.order.core.special.sql.SpecialOrderSql;
import com.dingding.order.dto.history.HistoryOrderDto;

@Component("specialHistoryOrderDao")
public class SpecialHistoryOrderDaoImpl implements IHistoryOrderDao{

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource(name = "SQLBaseDao")
	private ISQLBaseDao sqlBaseDao;
	
	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;
	
	@Override
	@Transactional(readOnly=true)
	public List<HistoryOrderDto> getHistoryOrders(String userId, Integer orderStatus, Integer pageIndex,
			Integer pageSize,Integer userType) {
		
		List<HistoryOrderDto> list = null;
		try {
			list = this.sqlBaseDao.listSqlToCustomBeanByPage(HistoryOrderDto.class, 
					SpecialOrderSql.getSpecialHistoryOrder(userType), pageIndex, pageSize,userId);
		} catch (Exception e) {
			log.error("获取专车历史订单出错！SpecialHistoryOrderDaoImpl : getHistoryOrders ()",e);
		}
		if(list == null){
			return new ArrayList<HistoryOrderDto>();
		}else{
			
			if(list.size() > 0){
				
				// 设置订单类型   1:接乘   2:搭乘
				for (HistoryOrderDto historyOrderDto : list) {
					
					historyOrderDto.setUserAvatar(ToolUtil.imageAddress(historyOrderDto.getUserAvatar()));
					historyOrderDto.setUserTabloidAvatar(ToolUtil.imageAddress(historyOrderDto.getUserTabloidAvatar()));
					
					if(userType == 1){
						historyOrderDto.setOrderType(1);
					}else if(userType == 2){
						historyOrderDto.setOrderType(2);
					}
				}
				
			}
			
			return list;
		}
	}

	/**
	 * @Description:获取历史订单
	 * @param userId 用户id 
	 * @param pageIndex 当前页
	 * @param pageSize	每页个数
	 * @author YHQ
	 * @return List<HistoryOrderDto> 历史订单
	 * @since 2.5.5
	 */
	@Override
	@Transactional(readOnly=true)
	public List<HistoryOrderDto> completeAndCancelOrder(String userId, Integer pageIndex,Integer pageSize,Integer userType) {
		
		List<HistoryOrderDto> list = null;
		try {
			list = this.sqlBaseDao.listSqlToCustomBeanByPage(HistoryOrderDto.class, 
					SpecialOrderSql.getSpecialMergeHistoryOrder(userType), pageIndex, pageSize,userId);
		} catch (Exception e) {
			log.error("获取专车历史订单出错！SpecialHistoryOrderDaoImpl : getHistoryOrders ()",e);
		}
		if(list == null){
			return new ArrayList<HistoryOrderDto>();
		}else{
			
			if(list.size() > 0){
				
				// 设置订单类型   1:接乘   2:搭乘
				for (HistoryOrderDto historyOrderDto : list) {
					
					historyOrderDto.setUserAvatar(ToolUtil.imageAddress(historyOrderDto.getUserAvatar()));
					historyOrderDto.setUserTabloidAvatar(ToolUtil.imageAddress(historyOrderDto.getUserTabloidAvatar()));
					
					if(userType == 1){
						historyOrderDto.setOrderType(1);
					}else if(userType == 2){
						historyOrderDto.setOrderType(2);
					}
				}
				
			}
			
			return list;
		}
	}

	
	/**
	 * 
	 * @Title: takeMileage 
	 * @Description: 接乘里程
	 * @param userId 用户Id
	 * @since V2.4.0
	 * @author YHQ 
	 * @return void    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public TakeMileageDTO takeMileage(String userId) {
		
		try {
			// 获取接乘里程
			List<TakeMileageDTO> takeMileageDTOs = sqlBaseDao.listSqlToCustomBean(TakeMileageDTO.class, HistoryOrderSQL.TAKE_MILEAGE, userId);
		
			if(takeMileageDTOs != null && takeMileageDTOs.size() > 0){
				
				return takeMileageDTOs.get(0);
			}
			
		} catch (Exception e) {
			log.error("获取接乘里程时出现异常  类："+this.getClass()+" takeMileage()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: takeMileage 
	 * @Description: 出租车接乘里程
	 * @param userId 用户Id
	 * @since V2.4.0
	 * @author YHQ 
	 * @return void    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public TakeMileageDTO taxiTakeMileage(String userId) {
		
		try {
			// 获取接乘里程
			List<TakeMileageDTO> takeMileageDTOs = sqlBaseDao.listSqlToCustomBean(TakeMileageDTO.class, HistoryOrderSQL.TAKE_TAXI_MILEAGE, userId);
		
			if(takeMileageDTOs != null && takeMileageDTOs.size() > 0){
				
				return takeMileageDTOs.get(0);
			}
			
		} catch (Exception e) {
			log.error("获取接乘里程时出现异常  类："+this.getClass()+" takeMileage()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getMileages 
	 * @Description: 获取乘客的接乘里程和搭乘里程
	 * @param userId 用户Id
	 * @author YHQ 
	 * @return TakeMileageDTO    返回类型
	 */
	@Override
	@Transactional(readOnly = true)
	public TakeMileageDTO getMileages(String userId) {
		
		try {
			// 获取接乘里程
			List<TakeMileageDTO> takeMileageDTOs = sqlBaseDao.listSqlToCustomBean(TakeMileageDTO.class, HistoryOrderSQL.GET_MILEAGES, userId);
		
			if(takeMileageDTOs != null && takeMileageDTOs.size() > 0){
				
				return takeMileageDTOs.get(0);
			}
			
		} catch (Exception e) {
			log.error("获取接乘里程时出现异常  类："+this.getClass()+" takeMileage()", e);
		}
		
		return null;
	}

}
